package jdbc;

import javax.swing.plaf.nimbus.State;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 聚合函数与分组
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        /*
            查看每个班级各多少人?列出人数和班级名字
         */
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            /*
                SELECT COUNT(*) count,c.name
                FROM student s
                JOIN class c ON s.class_id=c.id
                GROUP BY c.name
             */
            String sql = "SELECT COUNT(*) count,c.name " +
                    "FROM student s " +
                    "JOIN class c ON s.class_id=c.id " +
                    "GROUP BY c.name";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                int count = rs.getInt("count");
                String name = rs.getString("c.name");
                System.out.println(count + "," + name);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
